package com.git.smp.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.SmpRoleMenuMapper;
import com.git.smp.entity.SmpRoleMenu;
import com.git.smp.entity.SmpRoleMenuExtend;
import com.git.smp.entity.SmpRoleMenuExtend;

@RestController
@RequestMapping("/roleMenu")
public class SmpRoleMenuController {
	
	Logger logger = LoggerFactory.getLogger(SmpRoleMenuController.class);
	
	@Autowired
	SmpRoleMenuMapper smpRoleMenuMapper;
	
	//获取角色对应菜单名称
	@RequestMapping("/getMenuNameByRoleId")
	public ResponseMessage<List<String>> getMenuNameByRoleId(@RequestParam String roleId){
		
		ResponseMessage<List<String>> responseMessage = new ResponseMessage<List<String>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("查询角色对应菜单名称信息成功！");
		
		List<String> listMenuName = smpRoleMenuMapper.getMenuNameByRoleId(roleId);
		
		if(listMenuName == null) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("查询角色对应菜单名称信息失败！");
		}
		
		responseMessage.setExtend(listMenuName);
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	//维护角色菜单信息
	@RequestMapping("/updateRoleMenu")
	@Transactional
	public void updateRoleMenu(@RequestBody SmpRoleMenuExtend smpRoleMenuExtend) {
		SmpRoleMenu smpRoleMenu = new SmpRoleMenu();
		smpRoleMenu.setRoleId(smpRoleMenuExtend.getRoleId());
		
		
		smpRoleMenuMapper.delByRoleId(smpRoleMenu.getRoleId());
		for (String name : smpRoleMenuExtend.getSelectMenuId()) {
			smpRoleMenu.setMenuName(name);
			smpRoleMenuMapper.insert(smpRoleMenu);
		}
	}
}
